package com.zmx.user.web;

import com.zmx.common.common.web.rest.ResponseResult;
import com.zmx.user.dto.MemberReceiverDto;
import com.zmx.user.service.MemberReceiverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 收货地址控制器
 * @ClassName: MemberReceiverController
 * @Author zhaomxr
 * @Date 2022-01-06 15:21
 */
@Api("收货地址控制器")
@Slf4j
@RestController
@RequestMapping("receiver")
public class MemberReceiverController {

    @Autowired
    private MemberReceiverService memberReceiverService;

    @ApiOperation(value = "获取该客户所有收货地址列表（最多保存10条收货地址）", tags = "获取该客户所有收货地址列表（最多保存10条收货地址）")
    @GetMapping(value = "getReceiver")
    public ResponseResult getReceiver() {
        try {
            List<MemberReceiverDto> memberReceiverDtos = memberReceiverService.getByMemberId();
            return ResponseResult.success(memberReceiverDtos);
        } catch (Exception e) {
            log.error("获取收货地址列表失败", e);
            return ResponseResult.fail("获取收货地址列表失败");
        }
    }

    @ApiOperation(value = "获取收货地址详细信息", tags = "获取收货地址详细信息")
    @GetMapping(value = "getReceiverDetail")
    public ResponseResult getReceiverDetailById(@RequestParam(required = true) Long id) {
        log.info("begin to getReceiverDetail id:" + id);
        try {
            MemberReceiverDto memberReceiverDto = memberReceiverService.getReceiverById(id);
            return ResponseResult.success(memberReceiverDto);
        } catch (Exception e) {
            log.error("获取收货地址列表失败", e);
            return ResponseResult.fail("获取收货地址列表失败");
        }
    }

    @ApiOperation(value = "根据memberId查找默认收货地址", tags = "根据memberId查找默认收货地址")
    @GetMapping("getReceiverByOpenId")
    public ResponseResult getReceiverDefault() {
        log.info("begin to getReceiverDefault");
        try {
            MemberReceiverDto memberReceiverDto = memberReceiverService.getReceiverDefault();
            return ResponseResult.success(memberReceiverDto);
        } catch (Exception e) {
            log.error("获取默认收货地址失败", e);
            return ResponseResult.fail("获取默认收货地址失败");
        }
    }

    @ApiOperation(value = "新增收货地址", tags = "新增收货地址")
    @PostMapping(value = "addReceiver")
    public ResponseResult addReceiver(MemberReceiverDto memberReceiverDto) {
        log.info("begin to addReceiver receiverDTO:" + memberReceiverDto.toString());
        try {
            memberReceiverService.addReceiver(memberReceiverDto);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("新增收货地址失败", e);
            return ResponseResult.fail("新增收货地址失败");
        }
    }

    @ApiOperation(value = "修改收货地址", tags = "修改收货地址")
    @PostMapping(value = "updateReceiver")
    public Object updateReceiver(MemberReceiverDto memberReceiverDto) {
        log.info("begin to updateReceiver receiverDTO: {}", memberReceiverDto.toString());
        try {
            memberReceiverService.updateReceiver(memberReceiverDto);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("修改收货地址失败", e);
            return ResponseResult.fail("修改收货地址失败");
        }
    }

    @ApiOperation(value = "删除收货地址", tags = "删除收货地址")
    @PostMapping(value = "deleteReceiver")
    public Object deleteReceiver(@RequestParam("id") Long id) {
        log.info("begin to deleteReceiver id: {}", id);
        try {
            memberReceiverService.deleteReceiver(id);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("删除收货地址失败", e);
            return ResponseResult.fail("删除收货地址失败");
        }
    }
}
